from bs4 import BeautifulSoup
import requests
import pymysql
import time

urls= ["https://www.xinshipu.com/chuyoufenlei/?page={}".format(str(i)) for i in range(1,21)]
data = []
#获取tag#
def get_categories(url):
    web_data = requests.get(url)
    soup = BeautifulSoup(web_data.text,'lxml')
    categories = soup.findAll(rel="chuyoufenlei")
    for category in categories:
        string = (category.get_text().split("(")[0].strip(),int(time.time()))
        data.append(string)
#链接数据库#
def connet_mysql(data):
    db = pymysql.connect(host="127.0.0.1",user="root",passwd='root',port=3306,charset="utf8")
    cur = db.cursor()
    cur.execute('use test')
    cur.executemany("insert into tag(tag_name,create_time)values(%s,%s)",data)
    db.close()
    
for url in urls:
    get_categories(url)

connet_mysql(data)


